﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Product : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadData();
        }
        else
        {
            if (Request.QueryString["VendorID"] != null)
            {
                LoadData(Int32.Parse(Request.QueryString["VendorID"]), 0);
            }
            else if (Request.QueryString["ProductTypeID"] != null)
            {
                LoadData(Int32.Parse(Request.QueryString["ProductTypeID"]),1);
            }
        }
    }

    private void LoadData(int p,int flag)
    {
        if (flag == 0)
        {
            ManagementProduct pro = new ManagementProduct();
            ListProduct.DataSource = pro.GetProductByVendorID(p);
            
            ListProduct.DataBind();
        }
        else
        {
            ManagementProduct pro = new ManagementProduct();
            ListProduct.DataSource = pro.GetProductByProductTypeID(p);
            
            ListProduct.DataBind();
        }
    }

    private void LoadData()
    {
        ManagementProduct pro = new ManagementProduct();
        RVendor.DataSource = pro.GetVendor();
        RProductType.DataSource = pro.GetProductType();
        ListProduct.DataSource = pro.GetProduct();
        DataBind();
    }

    protected void ListProduct_ItemCommand(object source, DataListCommandEventArgs e)
    {
        if (e.CommandName.Equals("select"))
        {
            Response.Redirect("ProductDetail.aspx?ProductID=" + ListProduct.SelectedValue);
        }else if(e.CommandName.Equals("addcart")){
            Response.Redirect("Cart.aspx?ProductID=" + ListProduct.SelectedValue);
        }
    }
    private void FlagMenuLeft(int VendorID, int ProductTypeID)
    {
        menu_active1.Value = VendorID.ToString();
        menu_active2.Value = ProductTypeID.ToString();
    }
}